'use strict'
const userService = require('service/user')
const response = require('util/response')
const validator = require('util/requestValidator')
const userValidation = require('validation/user')
module.exports = {
  
  /**
   *  @description: 用户注册
   */
  register: async(ctx, next) => {
    const param = ctx.input.fields || ctx.input
    await validator.validate(
      param,
      userValidation.register.schema,
      userValidation.register.options
      )
    const result = await userService.addUser(param)
    return response.map(ctx, result)
  },

  /** 
   * @description: 用户登录
  */
 login: async(ctx, next) => {
    const param = ctx.input.fields || ctx.input
    await validator.validate(
      param,
      userValidation.login.schema,
      userValidation.login.options
    )
    const result = await userService.findUser(param)
    return response.map(ctx, result)
  },

  /** 
   * @description: 修改密码
   */
  update: async(ctx, next) => {
    const param = ctx.input.fields || ctx.input
    await validator.validate(
      param,
      userValidation.update.schema,
      userValidation.update.options
    )
    const result = await userService.resetPassword(param)
    return response.map(ctx, result)
  }
}
